<?php
$data = array();

// readmore: http://legacy.datatables.net/usage/columns
// dom: http://datatables.net/release-datatables/examples/basic_init/dom.html
// header format with (key:value) <=> (field:name)
$_header = array(
	'role_name' => array(
		'name' => "First name"
	),
	'role_description' => array(
		'name' => "Last name"
	),
	'check' => array(
		'name' => "",
		'type' => "checkbox",
		'attr' => array(
			'check-all' => true,
			'target' => ".checked_row"
		)
	)
);

$_format_cols = array(
	array(
		'aTargets' => array(0), // can put to array is field in select from database
		'mData' => 'role_name',
		'bSortable' => true
	),
	 
	array(
		'aTargets' => array(-1),
		'bSortable' => false,
		'mData' => null,
		'sDefaultContent' => "<input type=\"checkbox\" class=\"checked_row\" />"
	),
	array(
		'aTargets' => array('_all'),
		'bSortable' => false
	)
);
$config_default = array(
    'header_icon' => @$this->resource->RES_ICON,
    'header_title' => 'Quản lý '.@$this->resource->RES_NAME,
    'add_title' => 'Thêm '.@$this->resource->RES_NAME,
    'ID' => '0',
	'table_name' => 'show_table',
    'showEdit' => has_permission($this->permissionEdit),
    'showDelete' => has_permission($this->permissionDelete),
    'showAdd' => has_permission($this->permissionCreate),
	'action' => array(
		'click' => array(
			'object' => ".edit-click2", //"#abc, #xyz, .click",
			'func' => 'datatable_edit'
		)
		//'change' => array()
	),
	'ajax' => array(
		'url' => $this->url_index . "/ajax_datatable",
		'async' => false
	)
);

if(!isset($config))
{
	$config = array();
}

$config = array_merge($config_default, $config);


 $btn_edit = '';
 $btn_delete = '';
 if($config['showEdit']) $btn_edit = '<a class="btn btn-default edit-click" href="javascript:void(0)"><i class="fa fa-pencil"></i> Edit</a>';
 if($config['showDelete']) $btn_delete = '<a href="#" title="Delete" data-toggle="modal" data-target="#delete" class="delete-row btn btn-default"> <i class="fa fa-trash-o text-danger"></i><span class="text-danger"> Delete</span></a>';
 if($config['showEdit'] ||  $config['showDelete'])
 {
 	 $format_cols[] = array(
		'aTargets' => array(-1),
		'bSortable' => false,
		'mData' => null,
		'sDefaultContent' =>$btn_edit.' '.$btn_delete
	);
 	  $header[] =array(
		'name' => ""
	);
 }

echo $this->atheme->data_table($header, $format_cols, $config);
?>
<script type="text/javascript">
	function datatable_edit(){
		// console.log("log-abc");
		// var data = table.row( $(this).parents('tr') ).data();
        // alert( data[0] +"'s salary is: "+ data[ 2] );
		// return false;
		//jQuery("#<?php echo $config['table_name'] ?>").DataTable().ajax.reload();
	}

</script>